package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.common.CommonResult;
import com.woniu.dto.DynamicCommentDTO;
import com.woniu.service.DynamicCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/dynamic_comment")
@Api(tags ="活动评论")
public class DynamicCommentController {
    @Autowired
    private DynamicCommentService dc_Service;

    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("sys:dc:list")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面数据条数",defaultValue = "5")
    })
    public CommonResult list(@RequestParam(value = "page",defaultValue = "1",required = false)Integer currentPage,
                             @RequestParam(value = "limit",defaultValue = "5",required = false)Integer pageSize){
        PageInfo info=null;
        try {
            List<DynamicCommentDTO> list=dc_Service.findByPage(currentPage,pageSize);
            info=new PageInfo(list);
            return CommonResult.success(info);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/delComment/{id}")
    @ResponseBody
    @RequiresPermissions("sys:dc:delete")
    @ApiOperation(value = "删除评论")
    @ApiImplicitParam(name = "id",value = "活动评论列表id",dataType = "Integer")
    public CommonResult delComment(@PathVariable("id")Integer id){
        try {
            dc_Service.delComment(id);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功!");
    }

    /**
     * 页面搜索
     * @param nickName
     * @param dynamicType
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    @ApiOperation("页面查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName",value = "用户名称"),
            @ApiImplicitParam(name = "dynamicType",value = "活动类型")
    })
    public CommonResult select(String nickName,Integer dynamicType){

        PageInfo info=null;
        try {
            List<DynamicCommentDTO> list=dc_Service.select(nickName,dynamicType);
            info=new PageInfo(list);
            return CommonResult.success(info);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }
}
